const router = require('koa-router')();
const mysqlApi = require('../lib/mysql');
const moment = require('moment')

//发表文章页面
router.get('/publish', async (ctx, next) => {
  await ctx.render('publish.ejs', {
    session: ctx.session,
  })
})

//发表文章
router.post('/publish', async (ctx, next) => {
  let data = ctx.request.body;
  let name = ctx.session.sessionUser
  let nameid = ctx.session.id
  let time = moment().format('YYYY-MM-DD HH:mm')
  if (data.title && data.content) {
    await mysqlApi.insertEssay_table([name, nameid, data.title, data.content, time]).then(win => {
      ctx.body = {
        code: 200,
        msg: '添加成功'
      }
    })
  } else {
    ctx.body = {
      code: 400,
      msg: '不能为空'
    }
  }

})

module.exports = router


